package co.edu.usbcali.ai.ga;

import java.util.ArrayList;
import java.util.List;



public class SimpleCromosom extends ArrayList<Gene> implements ICromosom{
	
	public CromosomType type;
	public static final int UNIFORM_SINGLE_CUT=0;	
	public static final int NO_SIZE=0;
	
	/* **********************************************
	 * CONSTRUCTORS
	 * **********************************************/
	
	/* **********************************************
	 * 
	 * **********************************************/


	public List<Gene> subCromosom(Integer p_start,Integer p_end){
		return this.subList(p_start.intValue(), p_end.intValue());
	}

	public List<Gene> subCromosom(Integer p_start){
		return this.subList(p_start.intValue(),size());
	}

	public boolean isCrossableWith(ICromosom p_partner){
		return (diferentGenes(p_partner)>1);
	}
	
	public int diferentGenes(ICromosom p_cromosom){
		int dif=0;
		int minSize=Math.min(size(),p_cromosom.size());
		for (int con=0;con<minSize;con++){
			if (!get(con).equals(((SimpleCromosom)p_cromosom).get(con))){
				dif++;
			}
		}
		return dif;
	}

	public boolean equals(Object p_cromosom){
		if (p_cromosom==null){
			return false;
		}else{
			SimpleCromosom other=(SimpleCromosom)p_cromosom;
			if (getType().equals(other.getType())){
				return super.equals(other);
/*				for(int con=0;con<size();con++){
					try{
						if (!get(con).equals(other.get(con))){
							return false;
						}
					}catch(IndexOutOfBoundsException iobe){
						return false;
					}
				}
				return true;
*/			}else{
				return false;
			}
		}
	}
	
	public int[] getIntArray(){
		int[] flo=new int[size()];
		int con=0;
		for (Gene gene : this) {
			flo[con]=((Integer)gene.getValue()).intValue();
			con++;
		}
		return flo;
	}
	
	/* **********************************************
	 * GETTERS AND SETTERS
	 * **********************************************/

	public CromosomType getType() {
		return type;
	}

	public void setType(CromosomType type) {
		this.type = type;
	}

	public String toString(){
		String ret="";
		for (Gene gene : this) {
			ret+=gene.toString()+" ";
		}
		return ret;
	}
}
